<?php
/**
 * 处理用户的注册请求
 *
 */
require_once( './path.php' );

//调用初始化页面
require_once( CLASSMATE_ROOT.'/controller/process_script_init.php' );

@$request = $_POST;

try{

if ( session::get('is_online') ){

	$response_message = array( 'type'=>'notice' ,
			 'content'=>'已经登录系统，无需再进行注册操作(可先退出登录再进行此操作)。' ,
			 'url_des'=>'退出登录？' ,
			 'url'=>'/classmate/controller/account/do_logout.php' );
			 
	session::set( 'response_message' , $response_message );
	
	header( 'Location: ../response_message.php' );
	exit();	
}

$db_connect = mongo_connect();
$user_reg_process = new user_reg_process( $db_connect );

if ( $user_reg_process->do_reg( $request ) ){	 
	 //注册成功
	 //session中保存的是email，当做username来使用。
	session::set('email' , $request['email']);
	
	$response_message = array( 'type'=>'info' , 'content'=>'注册成功！' , 'url'=>'account/login.php' );
	session::set( 'response_message' , $response_message );
	
	header( 'Location: ../response_message.php' );
	exit();
		
}else{
//无法预料的注册失败
	 throw new Exception( '无法预料原因的注册失败.' );
}

}catch( Exception $e ){
	 
	$reg_fail_message = array( 'type'=>'error' , 'content'=>$e->getMessage() );

	session::set('reg_fail_message' , $reg_fail_message );
	header( 'Location: reg.php' );
}
